package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    //@RequiredPermission(name = {"activity:list","活动页面"})
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }

    @RequestMapping("/delete")
    //@RequiresPermissions(value = {"activity:delete","活动删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            activityService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value = {"activity:saveOrUpdate","活动新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Activity activity){
        if (activity.getId() != null) {
            activityService.update(activity);
        }else {
            activityService.save(activity);
        }
        return new JsonResult();
    }
    @RequestMapping("/input")
    //@RequiresPermissions(value = {"activity:saveOrUpdate","活动新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult input(){

        return new JsonResult();
    }


    //直接定位到投票详情页
    @RequestMapping("/item")
    public String item(Model model,Long id){
        //通过id找到详情的数据
        Activity activity = activityService.selectById(id);
        model.addAttribute("activity",activity);
        return "/activity/vote";
    }




}
